﻿using System;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.Threading;
using log4net;

namespace RIBA.Web.Services
{
	[EnableClientAccess(RequiresSecureEndpoint = false)]
	public class DomainServiceRIBA : DomainService
	{
		private static readonly ILog _log = LogManager.GetLogger(typeof (DomainServiceRIBA));

		[Invoke]
		[RequiresAuthentication(ErrorMessage = "RequiresAuthentication", ResourceType = typeof(TextRes))]
		public string GetServerTime(int sleep)
		{
			try
			{
				_log.DebugFormat("GetServerTime: sleep='{0}'", sleep);

				Thread.Sleep(sleep);
				return DateTime.Now.ToLongTimeString();
			}
			catch (Exception ex)
			{
				_log.Fatal(string.Format("Error in GetServerTime. sleep='{0}'", sleep), ex);
				return "";
			}
		}

		[Invoke]
		[RequiresAuthentication(ErrorMessage = "RequiresAuthentication", ResourceType = typeof(TextRes))]
		[RequiresRole("admin", ErrorMessage = "NoAccessRight", ResourceType = typeof(TextRes))]
		public string GetLongAnswer(int stringLength)
		{
			try
			{
				_log.DebugFormat("GetLongAnswer: stringLength='{0}'", stringLength);

				var s = new string('i', stringLength);
				return s;
			}
			catch (Exception ex)
			{
				_log.Fatal(string.Format("Error in GetLongAnswer. stringLength='{0}'", stringLength), ex);
				return "";
			}
		}
	}
}


